package com.w.proxy.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JDKProxy implements InvocationHandler {
    private Object cls;

    public JDKProxy(Object cls) {
        this.cls = cls;
    }

    /**
     * @param <T> 泛型
     * @return 代理实例 proxy對象
     */
    public <T> T getProxy() {
        return (T) Proxy.newProxyInstance(this.cls.getClass().getClassLoader(),
                this.cls.getClass().getInterfaces(),
                this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result = method.invoke(this.cls, args);
        return result;
    }
}
